import regular from './regular';
import composition from './composition';

/** 字符串类型校验 */
export const isString = (value) => typeof value === 'string';

/** 手机号校验 */
export const isPhone = (value) => regular.EXP_PHONE.test(value);

/** 邮箱校验 */
export const isEMail = (value) => regular.EXP_EMAIL.test(value);

/** 身份证校验 */
export const isIDCard = (value) => {
  const { checkProv, checkDate, checkCode } = composition;
  if (checkCode(value)) {
    var date = value.substring(6, 14);
    if (checkDate(date)) {
      if (checkProv(value.substring(0, 2))) {
        return true;
      }
    }
  }
  return false;
}

/** 非值校验
 * @param {any} value 校验值
 * @param {boolean} clearSpace 是否清空空字符，value是字符串时有效，默认 false
 * @returns true-非值，false-有效值
 */
export const isEmpty = (value = null, clearSpace = false) => {
  // 对常见的“非”值(不包含bool型)，直接返回原来值
  const empty = [null, undefined, NaN, ""]
  if (empty.includes(value)) return true

  // 清理空格、制表符后再校验
  if (isString(value) && clearSpace) {
    return value.replace(/\s+/g, '').length == 0
  }

  return false
}

/** 非值校验(多值)
 * @param values 校验集合 []
 * @param clearSpace 是否清空空字符，value是字符串时有效，默认 false
 * @returns true-非值，false-有效值
 */
export const isEmptys = (values, clearSpace = false) => {
  if (values instanceof Array) {
    return !values.length || values.some(value => isEmpty(value, clearSpace))
  }
  return true
}

/** 空对象校验 */
export const isEmptyObj = (value) => [null, undefined].includes(value) ? true : Object.keys(value).length == 0;

/** 校验是否是数字类型 */
export const isNumber = value => !isNaN(value / 1) && typeof value === 'number';

/** 校验是否是boolean类型 */
export const isBoolean = value => typeof value === 'boolean';

/** 校验是否是Array */
export const isArray = value => value instanceof Array;

/** 校验是否是null */
export const isNull = value => value === null;

/** 校验是否是undefined */
export const isUnde = value => value === void 0;

/** 校验是否是ios系统 */
export const isIos = () => {
	const system = uni.getSystemInfoSync()
	return system.osName === 'ios'
}

export default {
  isPhone,
  isEMail,
  isIDCard,
  isEmpty,
  isEmptys,
  isEmptyObj,
  isString,
  isBoolean,
  isNumber,
  isArray,
  isNull,
  isUnde,
  isIos
}
